<?php
/*
Plugin Name: Advanced Meta Plugin
Plugin URI: http://wordpress.org/extend/plugins/hello-dolly/
Description: This is a test plugin so I can lean what to do!
Author: Tyler Wiest
Version: 1.1
Author URI: http://www.jtwventures.com
License: GPLv2
*/

// use widgets_init action hook to execute custom function
add_action( 'add_meta_boxes', 'jtw_mbe_image_create' );

// register our widget
function jtw_mbe_image_create() {
	// create a custom meta box
	add_meta_box('jtw-image-meta', 'Set Image', 'jtw_mbe_image_function', 'post', 'normal', 'high');
}

//jtw_testwidget_widget_my_info class
function jtw_mbe_image_function( $post ) {
	
	// retreive the metadata values if they exist
	$jtw_mbe_image = get_post_meta( $post->ID, '_jtw_mbe_image', true);
	
	?>
	Image: <input type="text" name="jtw_mbe_image" id="jtw_mbe_image" size="50"
		value="<?php echo esc_attr( $jtw_mbe_image ); ?>" />
	<input id="upload_image_button" type="button" 
		value="Media Library Image" class="button-secondary" />
	<br />Enter an image URL or use an image from the Media Library
	<?php	
}

//script actions with page detection
add_action('admin_print_scripts-post.php', 'jtw_mbe_image_admin_scripts');
add_action('admin_print_scripts-post-new.php', 'jtw_mbe_image_admin_scripts');

function jtw_mbe_image_admin_scripts() {
	wp_enqueue_script( 'jtw-image-upload', 
		plugins_url('/adv_meta_box/jtw-meta-image.js'), 
		array('jquery','media-upload', 'thickbox')
		);
}

//style actions with page detection
add_action('admin_print_styles-post.php', 'jtw_mbe_image_admin_styles');
add_action('admin_print_styles-post-new.php', 'jtw_mbe_image_admin_styles');

function jtw_mbe_image_admin_styles() {
	wp_enqueue_style('tickbox');
}


// hook to save the metabox data
add_action( 'save_post', 'jtw_mbe_image_save_meta');

function jtw_mbe_image_save_meta( $post_id ) {
	// verify the meta data is set
	if ( isset($_POST['jtw_mbe_image'] ) ) {
		//save the metadata
		update_post_meta( $post_id, '_jtw_mbe_image',
			esc_url_raw( $_POST['jtw_mbe_image']));
	}
}


?>